package com.woniu.smart.service.impl;

import com.github.pagehelper.PageHelper;
import com.woniu.smart.dto.FollowDTO;
import com.woniu.smart.entity.Customer;
import com.woniu.smart.entity.Follow;
import com.woniu.smart.entity.SysAdmin;
import com.woniu.smart.mapper.CustomerMapper;
import com.woniu.smart.mapper.FollowMapper;
import com.woniu.smart.mapper.SysAdminMapper;
import com.woniu.smart.service.FollowService;
import com.woniu.smart.vo.FollowVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 *
 * @description：TODO
 *
 * @author     ：mmzs
 * @date       ：2022/10/25 9:20
 */
@Service
public class FollowServiceImpl implements FollowService {
    @Autowired
    private FollowMapper followMapper;
    @Autowired
    private SysAdminMapper sysAdminMapper;
    @Autowired
    private CustomerMapper customerMapper;
    @Override
    public List<FollowVO> selectAllByCustomerId(FollowDTO followDTO) {
        PageHelper.startPage(followDTO.getCurrentPage(),followDTO.getPageSize());
        return followMapper.selectAllByCustomerId(followDTO);
    }

    @Override
    public int deleteByPrimaryKey(Long id) {
        return followMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(Follow record) {
        return 0;
    }

    @Override
    public int insertSelective(FollowVO recordVO) {
        Customer customer = customerMapper.selectByPrimaryKey((long)recordVO.getCustomerId());
        customer.setCustomerDengji(recordVO.getCustomerDengji());
        Follow follow = new Follow();
        follow.setEmpId(sysAdminMapper.selectByEmpName(recordVO.getEmpName()));
        follow.setFollowState(recordVO.getFollowState());
        follow.setFollowBy2(recordVO.getFollowBy2());
        follow.setFollowTimes(recordVO.getFollowTimes());
        follow.setFollowContent(recordVO.getFollowContent());
        follow.setFollowDatetime(new Date());
        follow.setCustomerId(recordVO.getCustomerId());
        int i = customerMapper.updateByPrimaryKeySelective(customer);
        int i1 = followMapper.insertSelective(follow);
        return (i+i1)/2;
    }

    @Override
    public Follow selectByPrimaryKey(Long id) {
        return followMapper.selectByPrimaryKey(id);
    }

    /**
     * 根据客户id查客户姓名，用以动态生成
     * @param recordDTO
     * @return
     */
    @Override
    public String selCustomentName(FollowDTO recordDTO) {
        return customerMapper.selectByPrimaryKey((long)recordDTO.getCustomerId()).getCustomerName();
    }

    @Override
    public FollowVO selectByFollowId(Long id) {
        return followMapper.selectByFollowId(id);
    }

    @Override
    public int updateByPrimaryKeySelective(Follow record) {
        return followMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(Follow record) {
        return 0;
    }
}
